/*     */ package com.zimbra.qa.unittest;
/*     */ 
/*     */ import com.zimbra.common.account.Key.AccountBy;
/*     */ import com.zimbra.common.account.Key.DistributionListBy;
/*     */ import com.zimbra.common.account.Key.DomainBy;
/*     */ import com.zimbra.common.account.ZAttrProvisioning.GalMode;
/*     */ import com.zimbra.common.util.Log;
/*     */ import com.zimbra.common.util.ZimbraLog;
/*     */ import com.zimbra.cs.account.Account;
/*     */ import com.zimbra.cs.account.DistributionList;
/*     */ import com.zimbra.cs.account.Domain;
/*     */ import com.zimbra.cs.account.Provisioning;
/*     */ import com.zimbra.cs.gal.GalGroupMembers;
/*     */ import com.zimbra.cs.ldap.LdapUtil;
/*     */ import com.zimbra.qa.unittest.prov.soap.GalTestUtil;
/*     */ import com.zimbra.qa.unittest.prov.soap.GalTestUtil.GSAType;
/*     */ import java.util.HashMap;
/*     */ import java.util.Map;
/*     */ import java.util.Set;
/*     */ import junit.framework.TestCase;
/*     */ import org.junit.AfterClass;
/*     */ import org.junit.Assert;
/*     */ import org.junit.BeforeClass;
/*     */ import org.junit.Test;
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ public class TestGalGroupMembers
/*     */   extends TestCase
/*     */ {
/*     */   private static final String ZIMBRA_DOMAIN = "zimbra.galgrouptest";
/*     */   private static final String ZIMBRA_GROUP = "zimbra-group";
/*     */   private static final String EXTERNAL_DOMAIN = "external.galgrouptest";
/*     */   private static final String EXTERNAL_GROUP = "external-group";
/*     */   private static final String USER = "user";
/*  61 */   private static boolean initialized = false;
/*  62 */   private static boolean allDone = false;
/*     */   
/*     */   public void setUp() throws Exception {
/*  65 */     if (!initialized) {
/*  66 */       init();
/*  67 */       initialized = true;
/*     */     }
/*     */   }
/*     */   
/*     */   public void tearDown() throws Exception {
/*  72 */     if (allDone) {
/*  73 */       cleanup();
/*  74 */       initialized = false;
/*     */     }
/*     */   }
/*     */   
/*     */   static enum ZimbraGroupMembers
/*     */   {
/*  80 */     ZIMBRA_GROUP_MEMBER_ACCOUNT_1(true), 
/*  81 */     ZIMBRA_GROUP_MEMBER_ACCOUNT_2(true), 
/*  82 */     NON_ZIMBRA_ADDR_1(false), 
/*  83 */     NON_ZIMBRA_ADDR_2(false);
/*     */     
/*     */     private boolean isZimbraAccount;
/*     */     
/*     */     private ZimbraGroupMembers(boolean isZimbraAccount) {
/*  88 */       this.isZimbraAccount = isZimbraAccount;
/*     */     }
/*     */     
/*     */     private boolean isZimbraAccount() {
/*  92 */       return this.isZimbraAccount;
/*     */     }
/*     */     
/*     */     private String getAddress() {
/*  96 */       String localPart = toString().toLowerCase();
/*  97 */       return this.isZimbraAccount ? TestUtil.getAddress(localPart, "zimbra.galgrouptest") : TestUtil.getAddress(localPart, "somedomain.com");
/*     */     }
/*     */     
/*     */     static String[] getAllMembersAsArray()
/*     */     {
/* 102 */       String[] members = new String[values().length];
/* 103 */       for (ZimbraGroupMembers member : values()) {
/* 104 */         members[member.ordinal()] = member.getAddress();
/*     */       }
/*     */       
/* 107 */       return members;
/*     */     }
/*     */     
/*     */     static void assertEquals(Set<String> addrs) {
/* 111 */       Assert.assertEquals(values().length, addrs.size());
/*     */       
/* 113 */       for (ZimbraGroupMembers member : values()) {
/* 114 */         Assert.assertTrue(addrs.contains(member.getAddress()));
/*     */         
/*     */ 
/* 117 */         Assert.assertTrue(addrs.contains(member.getAddress().toUpperCase()));
/* 118 */         Assert.assertTrue(addrs.contains(member.getAddress().toLowerCase()));
/*     */       }
/*     */     }
/*     */   }
/*     */   
/*     */   static enum ExternalGroupMembers {
/* 124 */     MEMBER_1, 
/* 125 */     MEMBER_2, 
/* 126 */     MEMBER_3, 
/* 127 */     MEMBER_4;
/*     */     
/*     */     private ExternalGroupMembers() {}
/* 130 */     private String getAddress() { String localPart = toString().toLowerCase();
/* 131 */       return TestUtil.getAddress(localPart, "external.galgrouptest");
/*     */     }
/*     */     
/*     */     static String[] getAllMembersAsArray() {
/* 135 */       String[] members = new String[values().length];
/* 136 */       for (ExternalGroupMembers member : values()) {
/* 137 */         members[member.ordinal()] = member.getAddress();
/*     */       }
/*     */       
/* 140 */       return members;
/*     */     }
/*     */     
/*     */     static void assertEquals(Set<String> addrs) {
/* 144 */       Assert.assertEquals(values().length, addrs.size());
/*     */       
/* 146 */       for (ExternalGroupMembers member : values()) {
/* 147 */         Assert.assertTrue(addrs.contains(member.getAddress()));
/*     */         
/*     */ 
/* 150 */         Assert.assertTrue(addrs.contains(member.getAddress().toUpperCase()));
/* 151 */         Assert.assertTrue(addrs.contains(member.getAddress().toLowerCase()));
/*     */       }
/*     */     }
/*     */   }
/*     */   
/*     */   private static void setupZimbraDomain() throws Exception {
/* 157 */     Provisioning prov = Provisioning.getInstance();
/*     */     
/*     */ 
/* 160 */     if (prov.get(Key.DomainBy.name, "zimbra.galgrouptest") == null) {
/* 161 */       ZimbraLog.test.info("Creating domain zimbra.galgrouptest");
/* 162 */       Domain domain = prov.createDomain("zimbra.galgrouptest", new HashMap());
/*     */       
/*     */ 
/* 165 */       Map<String, Object> attrs = new HashMap();
/* 166 */       domain.setGalMode(ZAttrProvisioning.GalMode.both, attrs);
/* 167 */       domain.addGalLdapURL("ldap://localhost:389", attrs);
/* 168 */       domain.setGalLdapBindDn("cn=config", attrs);
/* 169 */       domain.setGalLdapBindPassword("zimbra");
/* 170 */       domain.setGalLdapSearchBase(LdapUtil.domainToDN("external.galgrouptest"));
/* 171 */       domain.setGalAutoCompleteLdapFilter("zimbraAccountAutoComplete");
/* 172 */       domain.setGalLdapFilter("zimbraAccounts");
/*     */       
/* 174 */       prov.modifyAttrs(domain, attrs);
/*     */     }
/*     */     
/*     */ 
/* 178 */     String userAddr = TestUtil.getAddress("user", "zimbra.galgrouptest");
/* 179 */     if (prov.get(Key.AccountBy.name, userAddr) == null) {
/* 180 */       prov.createAccount(userAddr, "test123", null);
/*     */     }
/*     */     
/*     */ 
/* 184 */     for (ZimbraGroupMembers member : ZimbraGroupMembers.values()) {
/* 185 */       if (member.isZimbraAccount()) {
/* 186 */         String addr = member.getAddress();
/* 187 */         if (prov.get(Key.AccountBy.name, addr) == null) {
/* 188 */           prov.createAccount(addr, "test123", null);
/*     */         }
/*     */       }
/*     */     }
/*     */     
/*     */ 
/* 194 */     String groupAddr = TestUtil.getAddress("zimbra-group", "zimbra.galgrouptest");
/* 195 */     DistributionList group = prov.get(Key.DistributionListBy.name, groupAddr);
/* 196 */     if (group == null) {
/* 197 */       group = prov.createDistributionList(groupAddr, new HashMap());
/* 198 */       prov.addMembers(group, ZimbraGroupMembers.getAllMembersAsArray());
/*     */     }
/*     */   }
/*     */   
/*     */   private static void cleanupZimbraDomain() throws Exception
/*     */   {
/* 204 */     Provisioning prov = Provisioning.getInstance();
/*     */     
/* 206 */     GalTestUtil.disableGalSyncAccount(prov, "zimbra.galgrouptest");
/*     */     
/*     */ 
/* 209 */     String userAddr = TestUtil.getAddress("user", "zimbra.galgrouptest");
/* 210 */     Account userAcct = prov.get(Key.AccountBy.name, userAddr);
/* 211 */     if (prov.get(Key.AccountBy.name, userAddr) != null) {
/* 212 */       prov.deleteAccount(userAcct.getId());
/*     */     }
/*     */     
/* 215 */     for (ZimbraGroupMembers member : ZimbraGroupMembers.values()) {
/* 216 */       if (member.isZimbraAccount()) {
/* 217 */         String addr = member.getAddress();
/* 218 */         Account acct = prov.get(Key.AccountBy.name, addr);
/* 219 */         if (acct != null) {
/* 220 */           prov.deleteAccount(acct.getId());
/*     */         }
/*     */       }
/*     */     }
/*     */     
/* 225 */     String groupAddr = TestUtil.getAddress("zimbra-group", "zimbra.galgrouptest");
/* 226 */     DistributionList group = prov.get(Key.DistributionListBy.name, groupAddr);
/* 227 */     if (group != null) {
/* 228 */       prov.deleteDistributionList(group.getId());
/*     */     }
/*     */     
/* 231 */     Domain domain = prov.get(Key.DomainBy.name, "zimbra.galgrouptest");
/* 232 */     if (domain != null) {
/* 233 */       ZimbraLog.test.info("Deleting domain zimbra.galgrouptest");
/* 234 */       prov.deleteDomain(domain.getId());
/*     */     }
/*     */   }
/*     */   
/*     */   private static void setupExternalDomain() throws Exception
/*     */   {
/* 240 */     Provisioning prov = Provisioning.getInstance();
/*     */     
/*     */ 
/* 243 */     if (prov.get(Key.DomainBy.name, "external.galgrouptest") == null) {
/* 244 */       ZimbraLog.test.info("Creating domain external.galgrouptest");
/* 245 */       prov.createDomain("external.galgrouptest", new HashMap());
/*     */     }
/*     */     
/*     */ 
/* 249 */     String groupAddr = TestUtil.getAddress("external-group", "external.galgrouptest");
/* 250 */     DistributionList group = prov.get(Key.DistributionListBy.name, groupAddr);
/* 251 */     if (group == null) {
/* 252 */       group = prov.createDistributionList(groupAddr, new HashMap());
/* 253 */       prov.addMembers(group, ExternalGroupMembers.getAllMembersAsArray());
/*     */     }
/*     */   }
/*     */   
/*     */   private static void cleanupExternalDomain() throws Exception {
/* 258 */     Provisioning prov = Provisioning.getInstance();
/*     */     
/* 260 */     String groupAddr = TestUtil.getAddress("external-group", "external.galgrouptest");
/* 261 */     DistributionList group = prov.get(Key.DistributionListBy.name, groupAddr);
/* 262 */     if (group != null) {
/* 263 */       prov.deleteDistributionList(group.getId());
/*     */     }
/*     */     
/* 266 */     Domain domain = prov.get(Key.DomainBy.name, "external.galgrouptest");
/* 267 */     if (domain != null) {
/* 268 */       ZimbraLog.test.info("Deleting domain external.galgrouptest");
/* 269 */       prov.deleteDomain(domain.getId());
/*     */     }
/*     */   }
/*     */   
/*     */ 
/*     */   @BeforeClass
/*     */   public static void init()
/*     */     throws Exception
/*     */   {
/* 278 */     setupZimbraDomain();
/* 279 */     setupExternalDomain();
/*     */   }
/*     */   
/*     */   @AfterClass
/*     */   public static void cleanup() throws Exception {
/* 284 */     cleanupZimbraDomain();
/* 285 */     cleanupExternalDomain();
/*     */   }
/*     */   
/*     */   private void doTest() throws Exception {
/* 289 */     String userAddr = TestUtil.getAddress("user", "zimbra.galgrouptest");
/* 290 */     Account acct = Provisioning.getInstance().get(Key.AccountBy.name, userAddr);
/*     */     
/* 292 */     Set<String> zimbraGroupMembers = GalGroupMembers.getGroupMembers(TestUtil.getAddress("zimbra-group", "zimbra.galgrouptest"), acct);
/* 293 */     ZimbraGroupMembers.assertEquals(zimbraGroupMembers);
/*     */     
/* 295 */     Set<String> externalGroupMembers = GalGroupMembers.getGroupMembers(TestUtil.getAddress("external-group", "external.galgrouptest"), acct);
/* 296 */     ExternalGroupMembers.assertEquals(externalGroupMembers);
/*     */   }
/*     */   
/*     */   @Test
/*     */   public void testLdapSearch() throws Exception {
/* 301 */     GalTestUtil.disableGalSyncAccount(Provisioning.getInstance(), "zimbra.galgrouptest");
/* 302 */     doTest();
/*     */   }
/*     */   
/*     */   @Test
/*     */   public void testGSASearch() throws Exception {
/* 307 */     GalTestUtil.enableGalSyncAccount(Provisioning.getInstance(), "zimbra.galgrouptest", GalTestUtil.GSAType.both);
/* 308 */     doTest();
/*     */   }
/*     */   
/*     */   @Test
/*     */   public void testLast() throws Exception
/*     */   {
/* 314 */     allDone = true;
/*     */   }
/*     */ }


/* Location:              /home/mint/zimbrastore.jar!/com/zimbra/qa/unittest/TestGalGroupMembers.class
 * Java compiler version: 7 (51.0)
 * JD-Core Version:       0.7.1
 */